package org.example.controller;

import org.example.common.result.Result;
import org.example.common.util.JwtUtils;
import org.example.entity.Account;
import org.example.entity.User;
import org.example.entity.UserDTO;
import org.example.mapper.UserMapper;
import org.example.service.AdminService;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@RestController
public class WebController {

    @Resource
    private  JwtUtils jwtUtils;

    @Resource
    private AdminService adminService;

    @Resource
    public UserService userService;
    @Resource
    private UserMapper userMapper;


    /**
     * 登录页面
     * @param
     * @return
     *
     */
    @PostMapping("/login")
    public  Result login(@RequestBody Account account){
        UserDTO result  =  userService.login(account);

        String token = JwtUtils.generateToken(result);
        // 构建返回结果
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("user", result);  // 可选：返回用于前端显示
        userMapper.updateLastLoginTime(result.getUserId());
        return Result.success(data);
    }

    /**
     * 用户注册
     * @param
     * @return
     */
    @PostMapping("/register")
    public  Result register(@RequestBody User user ){
       userService.register(user);
        return Result.success();
    }

//    /**
//     * 校验密码
//     */
//    @PutMapping("/updatePassword")
//     public Result updatePassword(@RequestBody Account account){
//
//        if("ADMIN".equals(account.getRole())){//管理员
//              adminService.updatePassword(account);
//        }else if("EMP".equals(account.getRole())) {
//              employeeService.updatePassword(account);
//        }else{
//            throw new CustomException("500","非法输入");
//        }
//        return Result.success();
//    }

//    @GetMapping("/barData")
//    public Result barData(){
//        Map<String,Object> map=new HashMap<>();
//        List<Employee> list = employeeService.selectAll(null);
//        Set<String> departmentNameSet = list.stream().map(Employee::getDepartmentName).collect(Collectors.toSet());
//        map.put("department",departmentNameSet);//x轴数据
//        List<Long> countList =new ArrayList<>();
//        for(String departmentName : departmentNameSet){
//            //统计部门员工数量
//            long count = list.stream().filter(employee -> employee.getDepartmentName().equals(departmentName)).count();
//            countList.add(count);
//        }
//        map.put("count",countList);//y轴数据
//        return Result.success(map);
//    }

}
